package com.fingard.dsp.bank.directbank.gzelink01.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Hex;

public class Md5 {
	public static String md5(String encryString, String Charset) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(encryString.getBytes(Charset));
			byte b[] = digest.digest();
			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			digest = null;
			b = null;
			return buf.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/*
	* 计算文件的MD5，重载方法
	* @param file 文件对象
	* @return
	* @throws IOException
	* @throws NoSuchAlgorithmException
	*/
	/*public static String getFileMD5String(File file) throws IOException{
		MessageDigest messageDigest = MessageDigest.getInstance("MD5");
		FileInputStream in = new FileInputStream(file);
		byte[] buffer = new byte[1024*1024];
		int len=0;
		while((len= in.read(buffer))>0){
			messageDigest.update(buffer, 0, len);
		}
		in.close();
		return bufferToHex(messageDigest.digest());
	}
	
	*//**
	 * 对一个字符串用指定算法生成字符串格式的信息摘要
	 * 
	 * @param str
	 * @return String
	 *//*
	public String getStrMD(String str) {
		if (str == null)
			return null;
		byte[] inByte = str.getBytes();
		byte[] outByte = getMD(inByte);
		String md = byteToStr(outByte);
		return md;
	}*/
	
	/**
	 * 对一个字节流生成字节流格式的信息摘要
	 * 
	 * @param inByte
	 * @return byte[]
	 */
	public byte[] getMD(byte[] inByte) {
		if (inByte == null) {
			return null;
		}
		byte[] hash = null;
		MessageDigest tMessageDigest;
		try {
			tMessageDigest = MessageDigest.getInstance("MD5");
			tMessageDigest.update(inByte);
			hash = tMessageDigest.digest();
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return hash;
	}
	
	/**
	 * @Des 得到相应的一个MD5加密后的字符串
	 * @param psd
	 * @param salt
	 * @return    MD5加密后的字符串
	 * @throws IOException 
	 */
	public static String encoder(String psd, String salt, String charset) throws IOException {
	    try {
	        StringBuffer stingBuffer = new StringBuffer();
	        // 1.指定加密算法
	        MessageDigest digest = MessageDigest.getInstance("MD5");
	        // 2.将需要加密的字符串转化成byte类型的数据，然后进行哈希过程
	        byte[] bs = digest.digest((psd + salt).getBytes(charset));
	        // 3.遍历bs,让其生成32位字符串，固定写法

	        // 4.拼接字符串
	        for (byte b : bs) {
	            int i = b & 0xff;
	            String hexString = Integer.toHexString(i);
	            if (hexString.length() < 2) {
	                hexString = "0" + hexString;
	            }
	            stingBuffer.append(hexString);
	        }
	        return stingBuffer.toString();
	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	public static String md5WithSalt(String encryString, String salt, String charset) throws Exception {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		byte[] bs = md5.digest((encryString + salt).getBytes(charset));
		encryString =  new String(new Hex().encode(bs), charset);
		
 		char[] cs = new char[48];
 		for (int i = 0; i < 48; i += 3) {
 			cs[i] = encryString.charAt(i / 3 * 2);
 			char c = salt.charAt(i / 3);
 			cs[i + 1] = c;
 			cs[i + 2] = encryString.charAt(i / 3 * 2 + 1);
 		}
		return new String(cs);
	}
}
